import useUserStore from '@/stores/user'
import axios from 'axios'

const request = axios.create({
  baseURL: import.meta.env.VITE_API_BASE_URL,
  timeout: 5000
})
// 添加请求拦截器
request.interceptors.request.use((config) => {
  // 在发送请求之前做些什么
  const userstore = useUserStore()
  const token = userstore.token
  if (token) {
    config.headers.token = token
    console.log(token, 'token')
  }
  return config
})
// 添加响应拦截器
request.interceptors.response.use(
  (response) => {
    return response
  },
  (error) => {
    return Promise.reject(error)
  }
)

const myaxios = (url: string, method: string, data?: any) => {
  return request({
    url,
    method,
    data
  })
}
export default myaxios
